package com.hqyj.lx.controller;

import com.hqyj.lx.pojo.Admin;
import com.hqyj.lx.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/*控制器*/
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    //页面通用跳转方法
    @RequestMapping("/{page}")
    public String toPage(@PathVariable()String page){
        return page;
    }

    //admin全查
    @RequestMapping("/adminQueryAll")
    public String AdminQueryAll(ModelMap modelMap){
        List<Admin> adminList = adminService.queryAll();
        modelMap.put("adminList",adminList);
        //返回视图名称
        return "/adminList";
    }


    //admin根据id查询单个
    @RequestMapping("/AdminQueryOne")
    public String AdminQueryOne(int sysId,ModelMap modelMap){
        Admin admin = adminService.queryOneBySysId(sysId);
        modelMap.put("admin",admin);
        return "update";
    }


    @RequestMapping("/AdminUpdateOne")
    //修改管理员信息
    public String AdminUpdateOne(Admin admin){
        int i = adminService.updateOne(admin);
        return "redirect:adminQueryAll";
    }


    @RequestMapping("/AdminInsertOne")
    //添加管理员信息
    public String AdminInsertOne(Admin admin){
        int i = adminService.insertOne(admin);
        return "redirect:adminQueryAll";
    }

    @RequestMapping("/AdminDeleteOne")
    //删除管理员信息
    public String AdminDeleteOne(int sysId){
        int i = adminService.deleteOne(sysId);
        return "redirect:adminQueryAll";
    }


    /*管理员登录*/
    @RequestMapping("/login")
    public ModelAndView login(String userName,String passWord, HttpSession session){
        //创建ModelAndView对象
        ModelAndView mv = new ModelAndView();
        System.out.println("用户名:" + userName + "密码:"+passWord);
        
        //调用service登录方法
        Admin login = adminService.login(userName,passWord);
        System.out.println(login);
        //判断数据库是否查询到前台页面输入的用户信息
        if(login != null){
            //密码判断,密码正确
            if(login.getSysPwd().equals(passWord)){
                //使用session进行数据共享
                session.setAttribute("admin",login);
                session.setAttribute("sysName",login.getSysName());
                session.setAttribute("sysId",login.getSysId());
                //请求转发
                mv.setViewName("forward:/index.jsp");
            }else {
                //密码错误
                session.setAttribute("error","用户名或密码错误");
                mv.setViewName("exampaperError");
            }
        }else {
            //查询为空,即不存在该用户
            session.setAttribute("error","用户名或密码不能为空");
            mv.setViewName("exampaperError");
        }
        //最终返回modelandview对象
        return mv;
    }

    //管理员注销
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        //清楚session共享的数据
        session.removeAttribute("admin");
        session.removeAttribute("sysName");
        session.removeAttribute("sysId");
        //重定向到登录界面
        return "redirect:/login.jsp";
    }


}
